{
 "cells": [
  {
   "cell_type": "raw",
   "id": "1988ec7e-3520-4ed9-9d7a-aa7ed18eb3b4",
   "metadata": {},
   "source": [
    "这里加雾的训练数据和test数据放在一起了。\n",
    "../../datasets/fogged/fogged_strength{test_strength}是测试集\n",
    "../../datasets/fogged/train_fogged_strength{test_strength}\n",
    "\n",
    "这个地方要干的就是从原始数据和加雾数据中抽取一定比例混合。这里目前只有test_strength = 0.6的。以后甚至可以加不同强度的进来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e5be6bcc-7838-4bab-875c-4c9d631ada46",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import cv2\n",
    "import random\n",
    "import subprocess\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "77c26825-1aff-4594-8098-27e7b075319f",
   "metadata": {},
   "outputs": [],
   "source": [
    "init_ratio = {\n",
    "    '0.6':0.3,\n",
    "}\n",
    "#不能比大，1减去剩下的部分是原始数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "42768603-544e-4eb2-a0cf-cb7aa4880292",
   "metadata": {},
   "outputs": [],
   "source": [
    "def mix_dataset(fogged_folder = '../../datasets/fogged/', \n",
    "                ratio = init_ratio,\n",
    "                train_folder = '../../datasets/kitti/images/origin_train', \n",
    "                out_folder = '../../datasets/kitti/images/train'\n",
    "               ):\n",
    "    \n",
    "    if sum([value for key,value in init_ratio.items()]) > 1.0:\n",
    "        print(\"ratio error\")\n",
    "        return \n",
    "    all_subdirs = [d for d in os.listdir(fogged_folder) if os.path.isdir(os.path.join(fogged_folder, d))]\n",
    "    test_strengths = [d.split('train_fogged_strength')[1] for d in all_subdirs if 'train_fogged_strength' in d]\n",
    "    \n",
    "    img_names = [img for img in os.listdir(train_folder) if img.endswith('.png')]\n",
    "    img_num = len(img_names)\n",
    "\n",
    "    random.shuffle(img_names)\n",
    "    begin = 0\n",
    "    end = 0;\n",
    "    for key, value in ratio.items():\n",
    "        end = int(begin + value * img_num)\n",
    "        for img_name in img_names[begin : end]:\n",
    "            cp_command = f\" \\\n",
    "                cp {fogged_folder}train_fogged_strength{key}/{img_name} {out_folder} \\\n",
    "            \" \n",
    "            subprocess.run(cp_command, shell=True, check=True)\n",
    "        begin = end\n",
    "    for img_name in img_names[begin : ]:\n",
    "        cp_command = f\" \\\n",
    "            cp {train_folder}/{img_name} {out_folder} \\\n",
    "        \" \n",
    "        subprocess.run(cp_command, shell=True, check=True)\n",
    "    \n",
    "        \n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c137c05e-f051-4641-9f19-308a1a6aea77",
   "metadata": {},
   "outputs": [],
   "source": [
    "mix_dataset()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efa13bbf-61db-45a6-864b-5b0ebf6d5926",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fd0883a-1d65-4c8e-9256-a03ad65acc4c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
